package com.ys.chatserver.tool;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.toolkit.ImageInfo;
import com.eva.framework.utils.LoggerFactory;
import com.ys.chatserver.BaseConf;
import org.slf4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;

public class FaceEngineUtils {

    private static final Logger logger = LoggerFactory.getLog();

    public static Boolean indentifyHumanFacePic(File file) {
        try {
            LoggerFactory.getLog().info("--------------------{}---------------------", file.getName());
            FaceEngine faceEngine = new FaceEngine(BaseConf.getInstance().HUMAN_FACE_LIB_PATH());
            //激活引擎
            int errorCode = faceEngine.activeOnline(BaseConf.getInstance().HUMAN_FACE_APPID(), BaseConf.getInstance().HUMAN_FACE_SDKKEY(), BaseConf.getInstance().HUMAN_FACE_ACTIVE_CODE());
            LoggerFactory.getLog().info("-------------ww-------{}---------------------", file.getName());

            faceEngine.activeOffline("/yisi/im/humanface/ArcFacePro64.dat");
            if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
                System.out.println("引擎激活失败");
            }

            LoggerFactory.getLog().info("-------------zx-------{}---------------------", file.getName());

            ActiveFileInfo activeFileInfo = new ActiveFileInfo();
            errorCode = faceEngine.getActiveFileInfo(activeFileInfo);
            if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
                System.out.println("获取激活文件信息失败");
            }
            LoggerFactory.getLog().info("---------------2-----{}---------------------", file.getName());
            //引擎配置
            EngineConfiguration engineConfiguration = new EngineConfiguration();
            engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
            engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
            engineConfiguration.setDetectFaceMaxNum(10);
            engineConfiguration.setDetectFaceScaleVal(16);
            //功能配置
            FunctionConfiguration functionConfiguration = new FunctionConfiguration();
            functionConfiguration.setSupportAge(true);
            functionConfiguration.setSupportFace3dAngle(true);
            functionConfiguration.setSupportFaceDetect(true);
            functionConfiguration.setSupportFaceRecognition(true);
            functionConfiguration.setSupportGender(true);
            functionConfiguration.setSupportLiveness(true);
            functionConfiguration.setSupportIRLiveness(true);
            engineConfiguration.setFunctionConfiguration(functionConfiguration);

            LoggerFactory.getLog().info("-------------3-------{}---------------------", file.getName());

            //初始化引擎
            errorCode = faceEngine.init(engineConfiguration);

            if (errorCode != ErrorInfo.MOK.getValue()) {
                System.out.println("初始化引擎失败");
            }
            LoggerFactory.getLog().info("---------------4-----{}---------------------", file.getName());

            //人脸检测
            ImageInfo imageInfo = getRGBData(file);
            List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
            errorCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
            System.out.println(faceInfoList);
            LoggerFactory.getLog().info("-------------5-------{}---------------------", file.getName());

            //引擎卸载
            errorCode = faceEngine.unInit();

            return !faceInfoList.isEmpty();
        }catch (Exception e){
            LoggerFactory.getLog().error(e.getMessage(),e);
            return false;
        }
    }
}